package com.sita.bike.utils;

import android.content.SharedPreferences;

import com.sita.bike.support.GlobalContext;
import com.sita.tboard.advertisement.AdModel;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by mark on 2015/3/3.
 */
public class LocalStorage {
    private final static String STATE_REFRESH_DATETIME = "state_refresh_datetime";
    private final static String UPDATE_GEO_DATETIME = "update_geo_datetime";
    private final static String FIRST_RUN_TAG = "first_run";
    private final static String LOGGED_IN = "logged_in";
    private final static String PUSH_TOKEN = "push_token";
    private final static String PUSH_DEVICE_ID = "push_device_id";
    private final static String SET_WAKEUP_FROM_BACKGROUND = "setWakeupFrom";
    private final static String AD_MODEL = "AD_MODEL";
    private final static String NO_ENABLE_BLUETOOTH_PROMPT = "no_enable_bluetooth_prompt";
    private final static String BLUETOOTH_ADDRESS = "bluetooth_address";
    private final static String RESOURCE_COUNT = "resource_count";
    private static final long EPOCH_START = 1420070400000l; //2015-01-01

    private static final SharedPreferences localStorage = GlobalContext.getLocalStorage();

    public static void setAdModel(AdModel adModel) {
        localStorage.edit().putString(AD_MODEL, adModel.toString()).apply();
    }

    public static AdModel getAdModel(){
        String adString = localStorage.getString(AD_MODEL, null);
        return adString != null ? AdModel.parse(adString) : null;
    }

    public static boolean isFirstRun() {
        return localStorage.getBoolean(FIRST_RUN_TAG, true);
    }

    public static boolean isLoggedIn() {
        return localStorage.getBoolean(LOGGED_IN, false);
    }

    public static void saveLastRefreshDatetime(long time) {
        localStorage.edit().putLong(STATE_REFRESH_DATETIME, time).apply();
    }

    public static long getLastRefreshDateTime() {
        return localStorage.getLong(STATE_REFRESH_DATETIME, 0);
    }

    public static void saveLastUpdateGeoDatetime(long time) {
        localStorage.edit().putLong(UPDATE_GEO_DATETIME, time).apply();
    }

    public static long getLastUpdateGeoDatetime() {
        return localStorage.getLong(UPDATE_GEO_DATETIME, 0);
    }

    public static void setLoggedIn(boolean isLogged) {
        localStorage.edit().putBoolean(LOGGED_IN, isLogged).apply();
    }

    public static void clearLocalStorage() {
        List<String> keys = new ArrayList<String>();
        Map map = localStorage.getAll();
        Iterator it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            String key = (String) entry.getKey();
            if (key.equals(PUSH_TOKEN) || key.equals(PUSH_DEVICE_ID) || key.equals("account")) {
                continue;
            } else {
                keys.add(key);
            }
        }
        for (String key : keys) {
            localStorage.edit().remove(key).apply();
        }
    }

    public static String getPushToken() {
        return localStorage.getString(PUSH_TOKEN, "");
    }

    public static void setPushToken(String token) {
        localStorage.edit().putString(PUSH_TOKEN, token).apply();
    }

    public static String getPushDeviceId() {
        return localStorage.getString(PUSH_DEVICE_ID, "");
    }

    public static void setWakeupFromBackground(boolean setwakeup) {
        localStorage.edit().putBoolean(SET_WAKEUP_FROM_BACKGROUND, setwakeup).apply();
    }

    public static void setId(long id) {
        localStorage.edit().putLong("imageId", id).apply();
    }

    public static long getId() {
        return localStorage.getLong("imageId", 0);
    }

    public static void setCurrentTimeMillis(long currentTimeMillis) {
        localStorage.edit().putLong("currentTimeMillis", currentTimeMillis).apply();
    }

    public static long getCurrentTimeMillis() {
        return localStorage.getLong("currentTimeMillis", 0);
    }

    public static void setAccountId(String accountId){
        localStorage.edit().putString("account", accountId).apply();
    }

    public static String getAccountId(){
        String accountId = localStorage.getString("account", null);
        return accountId;
    }

    public static void setEnableBluetoothPrompt(boolean value) {
        localStorage.edit().putBoolean(NO_ENABLE_BLUETOOTH_PROMPT, value).apply();
    }

    public static boolean getBluetoothPromptEnabled() {
        return localStorage.getBoolean(NO_ENABLE_BLUETOOTH_PROMPT, true);
    }

    public static void setBluetoothAddress(String address) {
        localStorage.edit().putString(BLUETOOTH_ADDRESS, address).apply();
    }

    public static String getBluetoothAddress() {
        return localStorage.getString(BLUETOOTH_ADDRESS, "");
    }

    public static void setRtRefreshTime(long time) {
        localStorage.edit().putLong("rt_refresh_time", time).apply();
    }

    public static long getRtRefreshTime() {
        return localStorage.getLong("rt_refresh_time", EPOCH_START);
    }

    public static void setResourceCount(long count) {
        localStorage.edit().putLong(RESOURCE_COUNT, count).apply();
    }

    public static long getResourceCount() {
        return localStorage.getLong(RESOURCE_COUNT, 0);
    }

    public static long getCheckUpdateTime(){
        return localStorage.getLong("check_update_time", EPOCH_START);
    }

    public static void updateCheckUpdateTime(long checkUpdateTime){
        localStorage.edit().putLong("check_update_time", checkUpdateTime).apply();
    }
}
